﻿using Newtonsoft.Json;
using System.Collections.Generic;
using System.ComponentModel;

namespace Framework.Repository
{
    /// <summary>
    /// 查询参数
    /// </summary>
    public class QueryCondition
    {
        public QueryCondition()
        {
            this.FilterList = new List<QueryFilter>();
        }

        /// <summary>
        /// 当前页数
        /// </summary>
        public int PageIndex { get; set; }

        /// <summary>
        /// 每页条数
        /// </summary>
        public int PageSize { get; set; }

        /// <summary>
        /// 排序字段
        /// </summary>
        public string SortBy { get; set; }

        /// <summary>
        /// True表示顺序，False表示倒序
        /// </summary>
        public bool SortOrder { get; set; }

        /// <summary>
        /// 排序字段
        /// </summary>
        public string ThenSortBy { get; set; }

        /// <summary>
        /// True表示顺序，False表示倒序
        /// </summary>
        public bool ThenSortOrder { get; set; }

        /// <summary>
        /// 过滤条件集合
        /// </summary>
        public string FilterListStr
        {
            get
            {
                return null;
            }
            set
            {
                if (!string.IsNullOrEmpty(value))
                {
                    FilterList = JsonConvert.DeserializeObject<List<QueryFilter>>(value);
                }
            }
        }

        /// <summary>
        /// 过滤条件集合
        /// </summary>
        public List<QueryFilter> FilterList { get; set; }

    }

    /// <summary>
    /// 列表页面进行查询的过滤条件
    /// </summary>
    public class QueryFilter
    {
        /// <summary>
        /// 过滤条件字段名
        /// </summary>
        public string FieldName { get; set; }

        /// <summary>
        /// 过滤条件值
        /// </summary>
        public object FieldValue { get; set; }

        /// <summary>
        /// 过滤类型
        /// </summary>
        public QueryMethod Operator { get; set; }

        /// <summary>
        /// 查询条件里本参数设置为一样的则为一组or查询，例：OrGroup = "group1"
        /// </summary>
        public string OrGroup { get; set; }

    }

    /// <summary>
    /// 过滤类型
    /// </summary>
    public enum QueryMethod
    {
        /// <summary>
        /// 等于
        /// </summary>
        [Description("=")]
        Equal = 0,

        /// <summary>
        /// 小于
        /// </summary>
        [Description("<")]
        LessThan = 1,

        /// <summary>
        /// 大于
        /// </summary>
        [Description(">")]
        GreaterThan = 2,

        /// <summary>
        /// 小于等于
        /// </summary>
        [Description("<=")]
        LessThanOrEqual = 3,

        /// <summary>
        /// 大于等于
        /// </summary>
        [Description(">=")]
        GreaterThanOrEqual = 4,

        /// <summary>
        /// 不等于
        /// </summary>
        [Description("<>")]
        NotEqual = 5,

        /// <summary>
        /// 开头等于
        /// </summary>
        [Description("like")]
        StartsWith = 6,

        /// <summary>
        /// 结尾等于
        /// </summary>
        [Description("like")]
        EndsWith = 7,

        /// <summary>
        /// 处理Like的问题
        /// </summary>
        [Description("like")]
        Contains = 8,

        /// <summary>
        /// In
        /// </summary>
        [Description("in")]
        StdIn = 9,

        /// <summary>
        /// Not In
        /// </summary>
        [Description("not in")]
        StdNotIn = 10,

        ///// <summary>
        ///// 为空
        ///// </summary>
        //[Description("is null")]
        //Null = 11,

        ///// <summary>
        ///// 不为空
        ///// </summary>
        //[Description("is not null")]
        //NotNull = 12,
    }

}